<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/1/9
 * Time: 15:26
 */

namespace app\cls\upload;

class MultiUpload extends AbstractUpload
{
    public function getFileDataInfo()
    {
        $dataInfo = [];
        foreach ($this->fileUrl as $key=>$fileUrl){
            $arr = [];
            $arr["file_path"] = $fileUrl;
            $arr["size"] = $this->size[$key];
            $arr["finalFileName"] = $this->finalFileName[$key];
            $dataInfo[] = $arr;
        }
        return $dataInfo;
    }

    /**
     * @return bool
     * @throws FileUploadException
     */
    public function doupload()
    {
        $this->init();
        $uploadPath = $this->uploadPath."/".$this->getSavePathSuffix();
        if(!$this->mkdir($uploadPath)){
            throw new FileUploadException("创建目录【{$uploadPath}】失败");
        }
        foreach($this->files as $fileInfo){
            $ext = $this->getFileExt($fileInfo);
            //获取redis唯一id
            $index = $this->redis->incrby(self::UPLOAD_INDEX,1);
            $finalFileName = md5($this->getSaveFileKey()."_".$index).".".$ext;
            $destination = $uploadPath."/". $finalFileName;
            $this->finalFileName[] = $finalFileName;
            $this->fileUrl[] = rtrim($this->saveHttp,"/")."/".$this->getSavePathSuffix()."/".$finalFileName;
            if($this->moveFileToSavePath($fileInfo["tmp_name"],$destination) === false){
                throw new FileUploadException("移动文件失败");
            }
        }
        return true;
    }
}